package com.henu.cee_service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.henu.cee_service.entity.News;
import com.henu.cee_service.entity.University;
import com.henu.cee_service.exception.BusinessException;
import com.henu.cee_service.exception.code.BaseResponseCode;
import com.henu.cee_service.mapper.UniversityMapper;
import com.henu.cee_service.service.RedisService;
import com.henu.cee_service.service.UniversityService;
import com.henu.cee_service.utils.PageUtil;
import com.henu.cee_service.vo.req.*;
import com.henu.cee_service.vo.resq.PageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * description: UniversityServiceImpl
 * date: 2020/7/28 17:43
 * author: JSM
 * version: 1.0
 */

@Service
@Slf4j
public class UniversityServiceImpl implements UniversityService {

    @Autowired
    private UniversityMapper universityMapper;

    @Autowired
    private RedisService redisService;

    @Override
    public PageVO<University> pageInfo(UniversityPageReqVO vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<University> list = universityMapper.selectAll(vo);
        return PageUtil.getPageVO(list);
    }

    @Override
    public PageVO<University> pageInfoByProvince(UniversityPageByProvinceReqVO vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<University> list = universityMapper.selectByProvince(vo);
        return PageUtil.getPageVO(list);
    }

    @Override
    public PageVO<University> pageInfoByScore(UniversityPageByScoreReqVO vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<University> list = universityMapper.selectByScore(vo);
        return PageUtil.getPageVO(list);
    }

    @Override
    public void addUniversity(UniversityAddReqVO vo) {
        University university = new University();
        BeanUtils.copyProperties(vo, university);
        int isSucces = universityMapper.insertSelective(university);
        if (1 != isSucces) {
            throw new BusinessException(BaseResponseCode.OPERATION_ERROR);
        }
    }

    @Override
    public void deleteUniversity(Long id) {
        universityMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateUniversity(UniversityUpdateReqVO vo) {
        University university = universityMapper.selectByPrimaryKey(vo.getId());
        if (null == university) {
            log.error("传入id:{} 不合法", vo.getId());
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        BeanUtils.copyProperties(vo, university);
        int isSuccess = universityMapper.updateByPrimaryKeySelective(university);
        if (1 != isSuccess) {
            throw new BusinessException(BaseResponseCode.OPERATION_ERROR);
        }
    }

    @Override
    public List<University> selectByName(String name) {
        List<University> list = universityMapper.selectByName(name);
        return list;
    }

    @Override
    public University selectById(Long id) {
        return universityMapper.selectByPrimaryKey(id);
    }
}
